$(function () {
    // console.log("恭喜来到本页面");
    // console.log($("#category_no").text());
    findWaresMessage(1,$("#category_no").text(),10);
});
//写一个寻找商品信息的函数
function findWaresMessage(currentPage,categoryNo,pageSize) {
    $.ajax({
        url:"/user/category/findWaresMessage",
        data:{
            currentPage:currentPage,
            categoryNo:categoryNo,
            pageSize:pageSize,
        },
        type:"PUT",
        success:function (resultInfo) {
            if(resultInfo.flag){
                var cid =currentPage;
                let i;
                // console.log(resultInfo);
                // console.log(resultInfo.data);

                $("#totalPage").html(resultInfo.data.totalPage);
                $("#totalCount").html(resultInfo.data.totalCount);

                var lis = "";
                var categoryNo = $("#category_no").text();
                var fristPage = '<li onclick="findWaresMessage('+1+','+categoryNo+','+10+')"><a href="javascript:void(0)">首页</a></li>';

                //计算上一页的页码
                var beforeNum =  resultInfo.data.currentPage - 1;
                if(beforeNum <= 0){
                    beforeNum = 1;
                }

                var beforePage = '<li  onclick="findWaresMessage('+beforeNum+','+categoryNo+','+10+')" ' +
                    'class="threeword"><a href="javascript:void(0)">上一页</a></li>';

                lis += fristPage;
                lis += beforePage;
                //1.2 展示分页页码
                /*
                    1.一共展示10个页码，能够达到前5后4的效果
                    2.如果前边不够5个，后边补齐10个
                    3.如果后边不足4个，前边补齐10个
                */

                // 定义开始位置begin,结束位置 end
                var begin; // 开始位置
                var end ; //  结束位置

                //1.要显示10个页码
                if(resultInfo.data.totalPage < 10){
                    //总页码不够10页
                    begin = 1;
                    end = resultInfo.data.totalPage;
                }else{
                    //总页码超过10页
                    begin = resultInfo.data.currentPage - 5 ;
                    end = resultInfo.data.currentPage + 4 ;
                    //2.如果前边不够5个，后边补齐10个
                    if(begin < 1){
                        begin = 1;
                        end = begin + 9;
                    }
                    //3.如果后边不足4个，前边补齐10个
                    if(end > resultInfo.data.totalPage){
                        end = resultInfo.data.totalPage;
                        begin = end - 9 ;
                    }
                }

                for (i = begin; i <= end ; i++) {
                    var li;
                    //判断当前页码是否等于i
                    if(resultInfo.data.currentPage == i){

                        li = '<li class="curPage" onclick="findWaresMessage('+i+','+categoryNo+','+10+')">' +
                            '<a href="javascript:void(0)">'+i+'</a></li>';

                    }else{
                        //创建页码的li
                        li = '<li onclick="findWaresMessage('+i+','+categoryNo+','+10+')"><a href="javascript:void(0)">'+i+'</a></li>';
                    }
                    //拼接字符串
                    lis += li;
                }
                var lastPage = '<li onclick="findWaresMessage('+resultInfo.data.totalPage+','+categoryNo+','+10+')" ' +
                    'class="threeword"><a href="javascript:void(0);">末页</a></li>';

                //计算下一页的页码
                var nextNum =  resultInfo.data.currentPage + 1;
                if(nextNum > resultInfo.data.totalPage){
                    nextNum = resultInfo.data.totalPage;
                }
                var nextPage = '<li  onclick="findWaresMessage('+nextNum+','+categoryNo+','+10+')" ' +
                    'class="threeword"><a href="javascript:void(0)">下一页</a></li>';

                lis += nextPage;
                lis += lastPage;

                //将lis内容设置到 ul
                $("#pageNum").html(lis);

                //2.列表数据展示
                var route_lis = "";

                for (i = 0; i < resultInfo.data.list.length; i++) {
                    //获取{rid:1,rname:"xxx"}
                    var route = resultInfo.data.list[i];

                    var li = '<li>\n' +
                        '    <div class="img"><img src="/img/product/small/m3554151406645ebce104f463c2c2e3c8c.jpg" style="width: 299px;"></div>\n' +
                        '    <div class="text1">\n' +
                        '     <p>'+route.name+'</p>\n' +
                        '       <br/>\n' +
                        '        <p>'+route.mark+'</p>\n' +
                        '        </div>\n' +
                        '        <div class="price">\n' +
                        '           <p class="price_num">\n' +
                        '        <span>&yen;</span>\n' +
                        '        <span>'+route.retailPrice+'</span>\n' +
                        '         <span>起</span>\n' +
                        '       </p>\n' +
                        '        <p><a href="/user/wares_list?no='+route.no+'">查看详情</a></p>\n' +
                        '          </div>\n' +
                        '       </li>';
                    route_lis += li;
                }
                $("#route").html(route_lis);

            }else {
                console.log("到底是发生了什么才会让你走到我这里来呢系思密达？");
            }
        }
    })
}

